package ca.badenhorst.ourtrition.model;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

/**
 * Created by wade on 26/02/14.
 */
public class Nutritions {
    //    general
    public static final String PROTEIN_TAG = "PROCNT";
    public static final String FAT_TAG = "FAT";
    public static final String CARB_TAG = "CHOCDF";
    public static final String KCAL_TAG = "ENERC_KCAL";
    public static final String KJOULES_TAG = "ENERC_KJ";
    public static final String FIBRE_TAG = "FIBTG";

    public static final String FAT_TRANS_TAG = "FATRN";
    public static final String FAT_SAT_TAG = "FASAT";
    public static final String FAT_POLY_UN_SAT_TAG = "FAPU";
    public static final String FAT_MONO_UN_SAT_TAG = "FAMS";
    public static final String CHOLESTEROL_TAG = "CHOLE";

    //    vitamins
    public static final String VIT_A_TAG = "VITA_IU";
    public static final String VIT_E_TAG = "TOCPHA";
    public static final String VIT_D_TAG = "VITD";
    public static final String VIT_C_TAG = "VITC";
    public static final String THIAMIN_TAG = "THIA";
    public static final String RIBOFLAVIN_TAG = "FIBF";
    public static final String NIACIN_TAG = "NIA";
    public static final String PANTOTHENIC_ACID_TAG = "PANTAC";
    public static final String VIT_B_6A_TAG = "VITB6A";
    public static final String VIT_B_12_TAG = "VITB12";
    public static final String CHOLINE_TAG = "CHOLN";
    public static final String VIT_K_TAG = "VITK1";
    public static final String FOLIC_ACID_TAG = "FOLAC";
    public static final String FOLATE_FOOD_TAG = "FOLFD";

    //    minerals
    public static final String CALCIUM_TAG = "CA";
    public static final String IRON_TAG = "FE";
    public static final String MAGNESIUM_TAG = "MG";
    public static final String PHOSPHORUS_TAG = "P";
    public static final String POTASSIUM_TAG = "K";
    public static final String SODIUM_TAG = "NA";
    public static final String ZINC_TAG = "ZN";
    public static final String COPPER_TAG = "CU";
    public static final String FLOURIDE_TAG = "FLD";
    public static final String MANGANESE_TAG = "MN";
    public static final String SELENIUM_TAG = "SE";

    Map<String, Nutrition> nutrients = new HashMap<String, Nutrition>();
    public static Set<String> minerals = new TreeSet<String>();
    public static Set<String> vitamins = new TreeSet<String>();

    public Nutritions() {
        minerals.add(CALCIUM_TAG);
        minerals.add(IRON_TAG);
        minerals.add(MAGNESIUM_TAG);
        minerals.add(PHOSPHORUS_TAG);
        minerals.add(POTASSIUM_TAG);
        minerals.add(SODIUM_TAG);
        minerals.add(ZINC_TAG);
        minerals.add(COPPER_TAG);
        minerals.add(FLOURIDE_TAG);
        minerals.add(MANGANESE_TAG);
        minerals.add(SELENIUM_TAG);

        vitamins.add(VIT_A_TAG);
        vitamins.add(VIT_E_TAG);
        vitamins.add(VIT_D_TAG);
        vitamins.add(VIT_C_TAG);
        vitamins.add(THIAMIN_TAG);
        vitamins.add(RIBOFLAVIN_TAG);
        vitamins.add(NIACIN_TAG);
        vitamins.add(PANTOTHENIC_ACID_TAG);
        vitamins.add(VIT_B_6A_TAG);
        vitamins.add(VIT_B_12_TAG);
        vitamins.add(CHOLINE_TAG);
        vitamins.add(VIT_K_TAG);
        vitamins.add(FOLIC_ACID_TAG);
        vitamins.add(FOLATE_FOOD_TAG);
    }

    public Set<Nutrition> getVitamins() {
        Set<Nutrition> result = new TreeSet<Nutrition>();
        for (Nutrition nutrition : getNutrients()) {
            if (vitamins.contains(nutrition.getTagname())) {
                result.add(nutrition);
            }
        }
        return result;
    }

    public Set<Nutrition> getMinerals() {
        Set<Nutrition> result = new TreeSet<Nutrition>();
        for (Nutrition nutrition : getNutrients()) {
            if (minerals.contains(nutrition.getTagname())) {
                result.add(nutrition);
            }
        }
        return result;
    }

    public Collection<Nutrition> getNutrients() {
        return nutrients.values();
    }

    public void setNutrients(List<Nutrition> nuts) {
        nutrients.clear();
        for (Nutrition nutrition : nuts) {
            nutrients.put(nutrition.getTagname(), nutrition);
        }
    }

    public void add(Nutrition nutrition) {
        nutrients.put(nutrition.getTagname(), nutrition);
    }

    public void append(Nutrition nutrition) {
        Nutrition nut = nutrients.get(nutrition.getTagname());
        if (nut != null) {
            nutrition.add(nut);
        }

        nutrients.put(nutrition.getTagname(), nutrition);
    }

    public Nutrition getNutrient(String name) {
        return nutrients.get(name);
    }

    @Override
    public String toString() {
        return "Nutritions{" +
                "nutrients=" + nutrients +
                '}';
    }

    public void addNutritions(Nutritions nutritions) {
        for (Nutrition nut : nutritions.getNutrients()) {
            append(nut);
        }
    }
}
